package LEETCODE2023;

import java.util.HashSet;
import java.util.Set;

public class Solution_1071 {
    public static void main(String[] args) {
//        System.out.println("senkwqne".substring(0, 3));
        System.out.println(gcdOfStrings("dsad", "ABBAABBA"));
    }


    public static String gcdOfStrings(String str1, String str2) {
        String str1gcd = findMinGCD(str1);
        String str2gcd = findMinGCD(str2);
        if (str1gcd.equals(str2gcd)) {
            return str1gcd;
        } else {
            return "";
        }
    }
//else {
//        if (str1gcd.contains(str2gcd)||str2gcd.contains(str1gcd)){
//
//        }
//        return findMinGCD(str2);
//}

    public static String findMinGCD(String str) {
        Set<Character> set = new HashSet<>();
        for (Character c : str.toCharArray()
        ) {
            set.add(c);
        }
        int size = set.size();
        StringBuilder temp = new StringBuilder(str.substring(0, size));
//        System.out.println("初始的字符串长度 " + temp.toString() + "  " + size);
        for (int i = set.size(); i < str.length(); i++) {
            if (str.substring(i, i + temp.length()).equals(temp.toString())) {
                break;
            } else {
                temp.append(str.charAt(i));
            }
        }
        return temp.toString();
    }
}
